package com.mjt.protal.web.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.mjt.dongbao.common.base.result.ResultWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Author mjt
 * @Time 2022/1/20
 */
@RestController
@RequestMapping("/kCaptcha")
public class KCaptchaController {

    Logger logger = LoggerFactory.getLogger(KCaptchaController.class);

    @Autowired
    private Kaptcha kaptcha;

    @GetMapping("/generator")
    public void generatorCode(HttpServletRequest request, HttpServletResponse response){
        // 生成验证码
        String render = kaptcha.render();
        logger.info("生成的验证码：" + render);
    }

    @GetMapping("/verifyCaptcha")
    public ResultWrapper verifyCode(String verifyCode, HttpServletRequest request){
        // 校验验证码
        boolean verifyResult = kaptcha.validate(verifyCode, 10);
        if (verifyResult){
            return ResultWrapper.getSuccessResult().msg("校验通过").build();
        }else {
            return ResultWrapper.getSuccessResult().msg("校验不通过").build();
        }
    }

}
